package model;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.sound.sampled.AudioFileFormat;

import datos.capturaaudio.core.SimpleAudioRecorder;
import datos.capturaaudio.exception.SimpleAudioRecorderException;
import datos.reproduccionaudio.core.SimpleAudioPlayer;
import datos.reproduccionaudio.exception.SimpleAudioPlayerException;

public class AudioManager {
	private SimpleAudioRecorder rec;
	private SimpleAudioPlayer play;
	
	public SimpleAudioRecorder getRec() {
		return rec;
	}
	public void setRec(SimpleAudioRecorder rec) {
		this.rec = rec;
	}
	public SimpleAudioPlayer getPlay() {
		return play;
	}
	public void setPlay(SimpleAudioPlayer play) {
		this.play = play;
	}
	public  AudioManager() throws InterruptedException, IOException{
		
	}
	
	//Reproduccion de la tira de bytes
	public void AudioPlay(byte[] Sonido) throws SimpleAudioPlayerException{
		InputStream iStream;
		iStream = new ByteArrayInputStream(Sonido);
		SimpleAudioPlayer play = new SimpleAudioPlayer(iStream);
		play.init();
		play.startPlaying();
		//play.startPlaying();
		try {
			play.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//Se obtiene la palabra en audio como una tira de bytes (ByteArrayOutputStream)
	public ByteArrayOutputStream WordRecord() throws SimpleAudioRecorderException, IOException {

		ByteArrayOutputStream recordedWord=null;
		
		OutputStream oStream = new ByteArrayOutputStream();
		SimpleAudioRecorder rec = new SimpleAudioRecorder(AudioFileFormat.Type.AU,oStream);
		this.setRec(rec);
			
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		String start = "a";
		rec.init();
		while (start.compareTo("i") !=0) {
			System.out.println("Ingrese 'i' para iniciar la grabacion.");
			start = input.readLine();
		}
		rec.startRecording();
		System.out.println("Grabando...");
		while (start.compareTo("f") !=0) {
			System.out.println("Ingrese 'f' para detener la grabacion.");
			start = input.readLine();
		}
		rec.stopRecording();			
		
		recordedWord = (ByteArrayOutputStream) rec.getOutput();
			
		this.getRec().getOutput().flush();
		this.getRec().getOutput().close();
		return recordedWord;
	}
	
	 
	//Se graba la palabra seleccionada. En caso de ser necesario, puede ser regrabada.
	public boolean AudioRecord(){
		
	try{
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			
		ByteArrayOutputStream byteArrayAudio=null;
		byteArrayAudio=this.WordRecord();

			String start = "a";
			while (start.compareTo("s") !=0) {
				System.out.println("Presione ENTER para escuchar la palabra grabada.");
				start = input.readLine();
				AudioPlay(byteArrayAudio.toByteArray());	
				while (start.compareTo("n") != 0) {
					System.out.println("Ingrese 'n' para repetir la grabacion o 's' para confirmarla.");
					start = input.readLine();
					if (start.compareTo("n") == 0) {
							byteArrayAudio.flush();
							byteArrayAudio.close();
							byteArrayAudio=this.WordRecord();
							System.out.println("Presione ENTER para escuchar la palabra grabada.");
							start = input.readLine();
							AudioPlay(byteArrayAudio.toByteArray());
							start = "a";
					}	
					if (start.compareTo("s") == 0)
						break;
				}
			}
			return true;
		}
		catch (Exception e) {
			System.out.println(e.toString());
			return false;
		}
	}

	

	
	
}


	



